package com.android.KickFlybug.GameObject;


import java.util.Random;

import com.android.KickFly;
import com.android.KickFlybug.Until.ActivityUtil;
import com.android.KickFlybug.Until.BitmapManager;
import com.android.R;




import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.provider.SyncStateContract.Constants;

//添加多种害虫，蚊子、苍蝇、蟑螂
public class Fly extends BaseGameObj {

	public static final int FLY_WIDTH = 65;
	public static final int FLY_HEIGHT = 60;
	public int mFlyType;
	public static final int TYPE_MOS = 1;//蚊子
	public static final int TYPE_FLY = 2;//苍蝇
	public static final int TYPE_COCKROACH = 3;//蟑螂

	private static final int MOVE_DIRECTION_LEFT = 0;
	private static final int MOVE_DIRECTION_RIGHT = 1;

	private static final int MOVE_DIRECTION_DOWN = 2;
	private static final int MOVE_DIRECTION_UP = 3;

	//static final Bitmap texture = BitmapManager.getBitmap(R.drawable.fly); // 害虫的纹理资源
	//static final Bitmap textureDead = BitmapManager
		//	.getBitmap(R.drawable.deadfly);

	static final Random rdm = new Random();
	static final Matrix m = new Matrix();

	public int mDirection = 0;// 当前害虫的飞行的方向
	private int count = 0;//方向改变时间间隔
	private Bitmap mTexture;
	private Bitmap mTextureDead;
	private Bitmap mBmpFly;
	private Bitmap mBmpFlyDead;
	public int moveOffset;

	public boolean dead = false;
	public boolean clean = false;//是否清扫死害虫
	//初始化函数
	//参数：
	//Bitmap bmpFly表示活的害虫的图片资源
	//Bitmap bmpFlyDead表示死的害虫的图片资源
	//int FlyType 1表示蚊子，2表示苍蝇，3表示蟑螂
	public Fly(Bitmap BmpFly,Bitmap BmpFlyDead, int FlyType) {
		w = FLY_WIDTH;
		h = FLY_HEIGHT;
		mFlyType = FlyType;
		mBmpFly = BmpFly;
		mBmpFlyDead = BmpFlyDead;
		switch (mFlyType) {
		case TYPE_MOS:
			moveOffset = 30;
			break;
		case TYPE_FLY:
			moveOffset = 20;
			break;
		case TYPE_COCKROACH:
			moveOffset = 5;
			break;
		default:
			break;
		}
		
		x = rdm.nextInt(ActivityUtil.SCREEN_WIDTH - FLY_WIDTH);
		y = rdm.nextInt(ActivityUtil.SCREEN_HEIGHT - FLY_HEIGHT);

		mDirection = rdm.nextInt(4);
		mTexture = getFrame(mDirection, mBmpFly);
		mTextureDead = getFrame(mDirection, mBmpFlyDead);
		visiable = true;
	}
	
	@Override
	public void paint(Canvas c) {
		mTexture = getFrame(mDirection, mBmpFly);
		mTextureDead = getFrame(mDirection, mBmpFlyDead);
		if (dead) {
			//清扫死害虫，不绘制死害虫图片
			if(!clean){//害虫死后绘制图片
			c.drawBitmap(mTextureDead, x, y, null);
			}
		} else {
			c.drawBitmap(mTexture, x, y, null);
		}
	}



	/**
	 * 移动
	 */
	public void move() {
		int moverdm;//任意移动的距离
		boolean changedir = false;
		if (dead) {
			return;
		}
		count++;
		moverdm = rdm.nextInt(2*moveOffset)-moveOffset;
		switch (mDirection) {
		case MOVE_DIRECTION_DOWN:
			y += moveOffset;
			x += moverdm;
			if (y + 5 + h >= ActivityUtil.SCREEN_HEIGHT) {
				mDirection = MOVE_DIRECTION_UP;
				changedir = true;
			}
			break;
		case MOVE_DIRECTION_UP:
			y -= moveOffset;
			x += moverdm;
			if (y + 5 <= 0) {
				mDirection = MOVE_DIRECTION_DOWN;		
				changedir = true;
			}
			break;
		case MOVE_DIRECTION_LEFT:
			x -= moveOffset;
			y += moverdm;
			if (x + 5 <= 0) {
				mDirection = MOVE_DIRECTION_RIGHT;
				changedir = true;
			}
			break;
		case MOVE_DIRECTION_RIGHT:
			x += moveOffset;
			y += moverdm;
			if (x + 5 + w >= ActivityUtil.SCREEN_WIDTH) {
				mDirection = MOVE_DIRECTION_LEFT;
				changedir = true;
			}
			break;
		}
		//没有到达边界，并且达到改变方向间隔(随机)
		if (changedir == false && count%(rdm.nextInt(20)+1)==0 ){
				mDirection = rdm.nextInt(4);
		}
	}

	public boolean contains(int mx, int my) {
		return mx > x && mx < x + w && my > y && my < y + h;
	}

	@Override
	public void logic() {
		move();//害虫移动
	}

	/**
	 * 获得对象指定方向上的帧图片
	 * 
	 * @param mDirection
	 *            方向
	 * @param b
	 *            原始图片
	 * @return
	 */
	private Bitmap getFrame(int mDirection, Bitmap b) {
		switch (mDirection) {
		case MOVE_DIRECTION_DOWN:
			m.setRotate(90);
			break;
		case MOVE_DIRECTION_UP:
			m.setRotate(270);
			break;
		case MOVE_DIRECTION_LEFT:
			m.setRotate(180);
			break;
		case MOVE_DIRECTION_RIGHT:
			m.setRotate(0);
			break;
		}
		return Bitmap.createBitmap(b, 0, 0, w, h, m, true);
	}

	/**
	 * 碰撞检测
	 * 
	 * @param fly
	 */
	public void collisionTo(Fly fly) {
		switch (mDirection) {
		case MOVE_DIRECTION_DOWN:
			if (fly.contains(x + 5, y + 5 + h)
					|| fly.contains(x + 5 + w, y + 5 + h)) {
				mDirection = MOVE_DIRECTION_LEFT;	
			}
			break;
		case MOVE_DIRECTION_UP:
			if (fly.contains(x + 5, y + 5) || fly.contains(x + 5 + w, y + 5)) {
				mDirection = MOVE_DIRECTION_DOWN;
			}
			break;
		case MOVE_DIRECTION_LEFT:
			if (fly.contains(x + 5, y + 5) || fly.contains(x, y + h + 5)) {
				mDirection = MOVE_DIRECTION_UP;
			}
			break;
		case MOVE_DIRECTION_RIGHT:
			if (fly.contains(x + 5 + w, y + 5)
					|| fly.contains(x + 5 + w, y + h + 5)) {
				mDirection = MOVE_DIRECTION_LEFT;
			}
			break;
		}
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}
}
